/**
 * Copyright 2010 Pavel Daniel Alexandru PFA
 * 
 * Licensed under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License. 
 */
package com.kobol.webclient.client.main.presenter;

import com.allen_sauer.gwt.log.client.Log;
import com.google.inject.Inject;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import com.gwtplatform.mvp.client.proxy.RevealRootLayoutContentEvent;
import com.kobol.webclient.client.NameTokens;
import com.kobol.webclient.client.widgets.ApplicationMenu;
import com.kobol.webclient.client.widgets.NavigationPane;
import com.kobol.webclient.client.widgets.NavigationPaneHeader;
import com.kobol.webclient.client.widgets.NavigationPaneSection;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.events.SectionHeaderClickEvent;
import com.smartgwt.client.widgets.layout.events.SectionHeaderClickHandler;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

/**
 * @author Pavel Alexandru
 * 
 */
public class MainPageSmartGwtPresenter extends
	Presenter<MainPageSmartGwtPresenter.MyView, MainPageSmartGwtPresenter.MyProxy> {

    private final PlaceManager placeManager;

    private static NavigationPaneHeader navigationPaneHeader;
    private static NavigationPane navigationPane;

    @ProxyCodeSplit
    @NameToken(NameTokens.mainSmartGwtPage)
    public interface MyProxy extends Proxy<MainPageSmartGwtPresenter>, Place {
    }

    public interface MyView extends View {
	ApplicationMenu getApplicationMenu();

	NavigationPaneHeader getNavigationPaneHeader();

	NavigationPane getNavigationPane();
    }

    /**
     * Use this in leaf presenters, inside their {@link #revealInParent} method.
     */
    @ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_SetContextAreaContent = new Type<RevealContentHandler<?>>();

    @Inject
    public MainPageSmartGwtPresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager) {
	super(eventBus, view, proxy);

	this.placeManager = placeManager;

	MainPageSmartGwtPresenter.navigationPaneHeader = view.getNavigationPaneHeader();
	MainPageSmartGwtPresenter.navigationPane = view.getNavigationPane();
    }

    @Override
    protected void onBind() {
	super.onBind();

	Log.debug("onBind()");

	// register Application Menu handler's
	getView().getApplicationMenu().addClickHandler("Sales", new ApplicationMenuClickHandler());

	// register Navigation Pane handler's
	getView().getNavigationPane().addSectionHeaderClickHandler(new NavigationPaneSectionHeaderClickHandler());
	getView().getNavigationPane().addRecordClickHandler("Sales", new NavigationPaneSectionClickHandler());
	getView().getNavigationPane().addRecordClickHandler("Settings", new NavigationPaneSectionClickHandler());
	getView().getNavigationPane().addRecordClickHandler("Resource Centre", new NavigationPaneSectionClickHandler());

	getView().getNavigationPane().expandSection("Sales");

	PlaceRequest myRequest = new PlaceRequest(NameTokens.activities);
	placeManager.revealPlace(myRequest);
    }

    @Override
    protected void onReveal() {
	super.onReveal();

	Log.debug("onReveal()");
    }

    @Override
    protected void onReset() {
	super.onReset();
    }

    @Override
    protected void revealInParent() {
	RevealRootLayoutContentEvent.fire(this, this);
    }

    public class ApplicationMenuClickHandler implements ClickHandler {
	@Override
	public void onClick(MenuItemClickEvent event) {

	    MenuItem menuItem = event.getItem();
	    String name = menuItem.getTitle();

	    Log.debug("ApplicationMenuClickHandler.onClick() - " + name);
	}
    }

    public class NavigationPaneSectionHeaderClickHandler implements SectionHeaderClickHandler {
	@Override
	public void onSectionHeaderClick(SectionHeaderClickEvent event) {

	    SectionStackSection section = event.getSection();
	    String name = ((NavigationPaneSection) section).getSelectedRecord();

	    Log.debug("NavigationPaneSectionHeaderClickHandler.onSectionHeaderClick() - " + name);

	    if (name.length() != 0) {
		PlaceRequest myRequest = new PlaceRequest(name);
		placeManager.revealPlace(myRequest);
	    }
	}
    }

    public class NavigationPaneSectionClickHandler implements RecordClickHandler {
	@Override
	public void onRecordClick(RecordClickEvent event) {

	    Record record = event.getRecord();
	    String name = record.getAttributeAsString("name");

	    Log.debug("NavigationPaneClickHandler.onRecordClick() - " + name);

	    PlaceRequest myRequest = new PlaceRequest(name);
	    placeManager.revealPlace(myRequest);
	}
    }

    public static NavigationPaneHeader getNavigationPaneHeader() {
	return navigationPaneHeader;
    }

    public static NavigationPane getNavigationPane() {
	return navigationPane;
    }
}